package cn.team11.smarteye.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import cn.team11.sds.dayi.R;

import org.apache.commons.lang3.StringUtils;

import cn.team11.smarteye.base.BaseDialog;
import cn.team11.smarteye.utils.ToastUtil;

/**
 * Created by Murphy on 2017/11/3.
 */

public class EditDialogFragment extends BaseDialog implements BaseDialog.OnBtClickListener{

    private EditText mRenameEt;
    private InputSubmitListener mListener;


    public EditDialogFragment() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_edit_content, container, false);
        mRenameEt = (EditText) view.findViewById(R.id.rename_edittext);
        setContent(view);
        setTitle(getActivity().getResources().getString(R.string.device_rename_txt));
        setOnBtClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onConfirmClick() {
        String newname = mRenameEt.getText().toString();
        if (!StringUtils.isEmpty(newname)){
            if (mListener != null){
                mListener.onSubmit(newname);
            }
            dismiss();
        } else {
            ToastUtil.showNegativeToast("新的昵称不能为空,请重新输入!");
        }
    }

    @Override
    public void onCancelClick() {
        mRenameEt.setText("");
        dismiss();
    }

    public void setSubmitListener(InputSubmitListener listener){
        this.mListener = listener;
    }

    public interface InputSubmitListener{
        void onSubmit(String name);
    }
}
